package org.robot.scm.platform.wxrobot.processor;

import com.google.common.collect.Lists;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;
import org.robot.scm.platform.wxrobot.AbstractRobotProcessor;
import org.robot.scm.pojos.TargetNotify;
import org.robot.scm.pojos.config.BaseConfig;
import org.robot.scm.pojos.config.base.BasePlatform;
import org.robot.scm.pojos.enums.PlatformEnum;
import org.robot.scm.utils.RequestHelper;

/**
 * TG 回复
 *
 * @Author : Awn create on 2024/4/22 15:52
 */
@Slf4j
public class TelegramRobotProcessor extends AbstractRobotProcessor {

    @Override
    public List<String> getKeywords(BaseConfig config) {
        return Lists.newArrayList("1", "已执行");
    }

    @Override
    protected List<TargetNotify> process(BaseConfig config, String sendTo, String msg) {
        // 发送者不是配置的机器人，忽略
        if (!config.get().getRobots(PlatformEnum.TELEGRAM).contains(sendTo)) {
            return Lists.newArrayList();
        }

        CompletableFuture.runAsync(() -> {
            try {
                BasePlatform platform = config.get().getPlatform(PlatformEnum.TELEGRAM);
                String url = String.format("%s/sendTg?msg=%s", platform.getNoticeSrc(), URLEncoder.encode(msg, StandardCharsets.UTF_8));
                String result = RequestHelper.get(url);
                log.info("telegram send result: {}", result);
            } catch (Exception e) {
                log.info("telegram send error: {}", e.getMessage());
            }
        });
        return Lists.newArrayList();
    }

}
